package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserChildSite;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserChildDaoImpl extends TempDataObjectDao<UserChildSite> {

    public Map<Integer, UserChildSite> getUserChildSiteMap(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, UserChildSite> dataMap = new ConcurrentHashMap<>();
        String sql = "select * from t_u_child_site where userId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserChildSite data = parse(rs);
                dataMap.put(data.getSiteIndex(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    @Override
    protected boolean addData(UserChildSite data) {
        PreparedStatement call = null;
        String sql = "insert into t_u_child_site(userId,siteIndex,trainTimes,lastRestoreTime,childUid,childTalent,childSex,childOccupation,childName," +
                "childHeadIcon,currentGrowth,motherBeautyId,trainType,trainCostSilver,childName2,childType,growAddEarnSpeed,growAddEarnSpeed2,`comprehend`) values" +
                "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setLong(i++, data.getUserId());
            call.setInt(i++, data.getSiteIndex());
            call.setInt(i++, data.getTrainTimes());
            call.setLong(i++, data.getLastRestoreTime());
            call.setString(i++, data.getChildUid());
            call.setInt(i++, data.getChildTalent());
            call.setInt(i++, data.getChildSex());
            call.setInt(i++, data.getChildOccupation());
            call.setString(i++, data.getChildName());
            call.setInt(i++, data.getChildHeadIcon());
            call.setInt(i++, data.getCurrentGrowth());
            call.setInt(i++,data.getMotherBeautyId());
            call.setInt(i++, data.getTrainType());
            call.setString(i++, data.getTrainCostSilver().toString());
            call.setString(i++,data.getChildName2());
            call.setInt(i++,data.getChildType());
            call.setString(i++, data.getGrowAddEarnSpeed().toString());
            call.setString(i++, data.getGrowAddEarnSpeed2().toString());
            call.setBoolean(i++, data.isComprehend());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(UserChildSite data) {
        PreparedStatement call = null;
        String sql = "UPDATE t_u_child_site SET trainTimes=?,lastRestoreTime=?,childUid=?,childTalent=?,childSex=?,childOccupation=?,childName=?," +
                "childHeadIcon=?,currentGrowth=?,motherBeautyId=?,trainType=?,trainCostSilver=?,childName2=?,childType=?,growAddEarnSpeed=?," +
                "growAddEarnSpeed2=?,`comprehend`=?  where userId=? and siteIndex=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setInt(i++, data.getTrainTimes());
            call.setLong(i++, data.getLastRestoreTime());
            call.setString(i++, data.getChildUid());
            call.setInt(i++, data.getChildTalent());
            call.setInt(i++, data.getChildSex());
            call.setInt(i++, data.getChildOccupation());
            call.setString(i++, data.getChildName());
            call.setInt(i++, data.getChildHeadIcon());
            call.setInt(i++, data.getCurrentGrowth());
            call.setInt(i++, data.getMotherBeautyId());
            call.setInt(i++, data.getTrainType());
            call.setString(i++, data.getTrainCostSilver().toString());
            call.setString(i++, data.getChildName2());
            call.setInt(i++, data.getChildType());
            call.setString(i++, data.getGrowAddEarnSpeed().toString());
            call.setString(i++, data.getGrowAddEarnSpeed2().toString());
            call.setBoolean(i++, data.isComprehend());
            call.setLong(i++, data.getUserId());
            call.setInt(i++, data.getSiteIndex());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected UserChildSite parseData(ResultSet rs) throws SQLException {
        UserChildSite data = new UserChildSite();
        data.setUserId(rs.getLong("userId"));
        data.setSiteIndex(rs.getInt("siteIndex"));
        data.setTrainTimes(rs.getInt("trainTimes"));
        data.setLastRestoreTime(rs.getLong("lastRestoreTime"));
        data.setChildUid(rs.getString("childUid"));
        data.setChildTalent(rs.getInt("childTalent"));
        data.setChildSex(rs.getInt("childSex"));
        data.setChildOccupation(rs.getInt("childOccupation"));
        data.setChildName(rs.getString("childName"));
        data.setChildHeadIcon(rs.getInt("childHeadIcon"));
        data.setCurrentGrowth(rs.getInt("currentGrowth"));
        data.setMotherBeautyId(rs.getInt("motherBeautyId"));
        data.setTrainType(rs.getInt("trainType"));
        String value = rs.getString("trainCostSilver");
        if(!StringUtils.isNullOrEmpty(value)){
            data.setTrainCostSilver(new BigInteger(value));
        }
        data.setChildName2(rs.getString("childName2"));
        data.setChildType(rs.getInt("childType"));
        value = rs.getString("growAddEarnSpeed");
        if(!StringUtils.isNullOrEmpty(value)){
            data.setGrowAddEarnSpeed(new BigInteger(value));
        }
        value = rs.getString("growAddEarnSpeed2");
        if(!StringUtils.isNullOrEmpty(value)){
            data.setGrowAddEarnSpeed2(new BigInteger(value));
        }
        data.setComprehend(rs.getBoolean("comprehend"));
        return data;
    }
}
